// pages/ticket/record/record.js
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var request = require('../../../utils/token.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Poiobject:{},
    thisObject: {},//Poiobject没值用这个
    stopAddress:'',
    address: "",
    showModal: false,
    isSubmit: false,
    isSub: false,
    warn: "",
    warn2: "",
    isPub: false,
    flag: 0,
    localhost:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.lookup(options);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.Poiobject)
    console.log(this.data.stopAddress)
    if (!this.data.stopAddress){//点击当前地址
          this.setData({
            stopAddress: this.data.address
          });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  lookup: function (options) {
    if (options.address != null && options.address != '') {
      //设置变量 address 的值
      this.setData({
        address: options.address
     //   localhost:options.
      });
    } else {
      qqmapsdk = new QQMapWX({
        //此key需要用户自己申请
        //  key: 'MNXBZ-G5TWD-GYF42-HHZJL-2W2J3-PVBX4'
        key: 'IM6BZ-HZORR-TRQW6-WUC5W-SDAWE-W4F5Q'

      });
      var that = this;
      qqmapsdk.reverseGeocoder({
        success: function (res) {
          console.log(res.result)
          that.setData({
            address: res.result.address,
            thisObject: res.result
          });
        },
        fail: function (res) {
          //console.log(res);
        },
        complete: function (res) {
          //console.log(res);
        }
      })
    }
  },
  geocoder:function(){

  
  },

  submit: function () {
    this.setData({
      showModal: true
    })
  },


  formSubmit: function (e) {
    let that = this
    console.log('form发生了submit1事件，携带数据为：', e.detail.value);
    let { ticketRequestFee, ticketValue, currentLocation, currentFloor,messageContent } = e.detail.value;
   
    if (!ticketRequestFee || !currentLocation) {
      this.setData({
        warn: "小费或位置为空！",
        isSubmit: true
      })
      return;
    }
    if(!that.moneyNumber(ticketRequestFee, ticketValue)) {
        return
    }
    //   ticketRequestFee,
    // ticketValue,
    //   currentLocation,
    //   currentFloor,
    //   messageContent
    this.setData({
      warn: "",
      isSubmit: false,
    })
    
  },
  formReset: function () {
    console.log('form发生了reset事件')
    this.setData({
      showModal: false
    })
  },

  formSub: function (e) {
    let that= this.data
    var market = {}
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    let { parketPayFee, parketRequestValue, currentLocation, currentFloor, messageContent} = e.detail.value;
    if (!parketPayFee || !currentLocation) {
      this.setData({
        warn2: "愿付的小费或位置为空！",
        isSub: true
      })
      return;
    }
    if (!that.moneyNumber(ticketRequestFee, ticketValue)) {
      return
    }
    this.setData({
      warn2: "",
      isSub: false,
    })
    market={
      marketId: that.Poiobject.id || that.thisObject.address_reference.famous_area.id,
      marketName: that.Poiobject.title || that.thisObject.address_reference.famous_area.title,
      //province:that.Poiobject.
    }
    // request.postRequest("/marketInfo/instMarketInfo")
    // request.postRequest("")
  },
  formRes: function () {
    console.log('form发生了reset事件')
    this.setData({
      showModal: false
    })
  },

  switchNav: function (e) {
    console.log(e.currentTarget.id);
    this.setData({
      flag: e.currentTarget.id
    })
  },



moneyNumber:function(e,r){
  //todo 要做验证只能输入数字
  var regNum = new RegExp('[0-9]', 'g');
  var rsNum = regNum.exec(e);
  var rrNum = regNum.exec(r);
  if (!rsNum) {
    setTimeout(() => {
      wx.showToast({
        title: '小费只能输入数字',
        icon: 'none'
      })
    }, 500);
    return false
  }else{
    if (!rrNum) {
      setTimeout(() => {
        wx.showToast({
          title: '小票面额只能输入数字',
          icon: 'none'
        })
      }, 500);
      return false
    }
  }
},

  stopAddress:function(){
    this.data.stopAddress=null,
    wx.navigateTo({
      url: '/pages/ticket/address/address'　　// 页面 A
    })
  }
})